/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager.helpers.validation;

import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import manager.auxiliares.Configurations;
import manager.auxiliares.FocusHelper;

/**
 *
 * @author Thiago
 */
public class PasswordFieldValidator extends JPasswordField {

    private int iMaxLength;
    private int iMinLength;
    private boolean isRequired;
    private boolean upper;
    private String validationType;
    private JLabel jLabel;
    private JPasswordField confirmPassword;

    public PasswordFieldValidator(int iMaxLength, int iMinLength, boolean isRequired, boolean upper, String validationType, JLabel jLabel) {
        super();
        this.iMaxLength = iMaxLength;
        this.iMinLength = iMinLength;
        this.isRequired = isRequired;
        this.upper = upper;
        this.validationType = validationType;
        this.jLabel = jLabel;
        this.confirmPassword = null;

        this.setDocument(new PasswordFieldValidationDocument(this));

        /**
         * Funcionalidade que seleciona todo o texto no evento de ganho de foco
         */
        this.addFocusListener(new java.awt.event.FocusAdapter() {

            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                ((JPasswordField) evt.getComponent()).selectAll();
            }
        });

        /**
         * Funcionalidade que passa para o próximo elemento com o pressionar da tecla enter
         */
        this.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyReleased(java.awt.event.KeyEvent evt) {
                int key = evt.getKeyCode();
                PasswordFieldValidator passwordFieldValidator = ((PasswordFieldValidator) evt.getComponent());
                if (passwordFieldValidator.validarCampo()) {
                    if (key == KeyEvent.VK_ENTER) {
                        FocusHelper.goToNextFocus();
                    }
                }
            }
        });
        this.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                PasswordFieldValidator passwordFieldValidator = ((PasswordFieldValidator) evt.getComponent());
                if (passwordFieldValidator.getToolTipText() == null) {
                    passwordFieldValidator.setToolTipText(passwordFieldValidator.getToolTip());
                }
            }
        });

    }

    public String getToolTip() {
        String toolTipText = (isRequired) ? "[Obrigatório]" : "";
        toolTipText += (iMaxLength != -1) ? "[Max:" + iMaxLength + "]" : "";
        toolTipText += (iMinLength != -1) ? "[Min:" + iMinLength + "]" : "";
        toolTipText = jLabel.getText() + ((!toolTipText.equals("")) ? ":" + toolTipText : "");
        return toolTipText;
    }

    public boolean validarCampo() {
        if (isRequired) {
            String str = this.getText();
            String strMatch = (confirmPassword != null) ? confirmPassword.getText() : null;
            boolean validated = ValidationFunctions.validade(str, strMatch, validationType, iMaxLength, iMinLength);
            if (validated) {
                jLabel.setForeground(Configurations.getFieldValidationOk());
                ((PasswordFieldValidator) confirmPassword).getjLabel().setForeground(Configurations.getFieldValidationOk());
            } else {
                jLabel.setForeground(Configurations.getFieldValidationNotOk());
                ((PasswordFieldValidator) confirmPassword).getjLabel().setForeground(Configurations.getFieldValidationNotOk());
                return false;
            }
        } else {
            jLabel.setForeground(Configurations.getFieldValidationOk());
            ((PasswordFieldValidator) confirmPassword).getjLabel().setForeground(Configurations.getFieldValidationOk());
        }
        return true;
    }

    public int getiMaxLength() {
        return iMaxLength;
    }

    public boolean isIsRequired() {
        return isRequired;
    }

    public JLabel getjLabel() {
        return jLabel;
    }

    public boolean isUpper() {
        return upper;
    }

    public void setConfirmPassword(JPasswordField confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
}
